package Dao.Impl;

import Dao.RentalRecordDAO;
import MyUtil.JDBCUtils;
import pojo.RentalRecord;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class RentalRecordDAOImpl implements RentalRecordDAO {

    public RentalRecordDAOImpl() throws Exception {
    }

    //通过用户id获取订单记录
    @Override
    public List<RentalRecord> getRentalRecordsByCustomerId(int customerId) throws Exception {
        //获取连接
        Connection rentalRecordDAOImplConn = JDBCUtils.getMyConnection();
        String sql;
        PreparedStatement ps;
        ResultSet rs;
        //查询SQL
        sql = "select * from rentalrecord where customer_id =?";
        ps = rentalRecordDAOImplConn.prepareStatement(sql);
        //占位符的赋值
        ps.setInt(1,customerId);
        rs = ps.executeQuery();
        List<RentalRecord> recordList = new ArrayList<>();
        //判断结果
        while (rs.next()){
            RentalRecord rentalRecord = new RentalRecord();

            rentalRecord.setRecord_id(rs.getInt(1));
            rentalRecord.setCustomer_id(rs.getInt(2));
            rentalRecord.setCar_id(rs.getInt(3));
            rentalRecord.setStart_date(rs.getString(4));
            rentalRecord.setEnd_date(rs.getString(5));
            rentalRecord.setRental_fee(rs.getInt(6));
            rentalRecord.setRental_status(rs.getString(7));
            rentalRecord.setRental_days(rs.getInt(8));

            recordList.add(rentalRecord);
        }
        //关闭资源
        JDBCUtils.release(rentalRecordDAOImplConn,ps,rs);
        return recordList;
    }

    //用户租车，增加订单
    @Override
    public Boolean addRentalRecord(RentalRecord rentalRecord) throws Exception {
        //获取连接
        Connection rentalRecordDAOImplConn = JDBCUtils.getMyConnection();
        String sql;
        PreparedStatement ps;
        //增加SQL
        sql = "insert into rentalrecord(customer_id, car_id, start_date, end_date, rental_fee, rental_status, rental_days) values(?,?,?,?,?,?,?)";
        ps = rentalRecordDAOImplConn.prepareStatement(sql);
        //占位符的赋值
        ps.setInt(1,rentalRecord.getCustomer_id());
        ps.setInt(2,rentalRecord.getCar_id());
        ps.setString(3,rentalRecord.getStart_date());
        ps.setString(4,rentalRecord.getEnd_date());
        ps.setInt(5,rentalRecord.getRental_fee());
        ps.setString(6,rentalRecord.getRental_status());
        ps.setInt(7,rentalRecord.getRental_days());
        int count = ps.executeUpdate();
        //判断结果
        if(count>0){
            return true;
        }
        //关闭资源
        JDBCUtils.release(rentalRecordDAOImplConn,ps,null);
        return false;
    }

    //用户在用户界面还车更新订单信息
    @Override
    public Boolean updateRentalRecord(RentalRecord rentalRecord) throws Exception {
        Connection rentalRecordDAOImplConn = JDBCUtils.getMyConnection();
        String sql;
        PreparedStatement ps;
        //查询SQL
        sql = "UPDATE rentalrecord set rental_status=?,end_date=? where record_id=?";
        ps = rentalRecordDAOImplConn.prepareStatement(sql);
        //占位符的赋值
        ps.setString(1,rentalRecord.getRental_status());
        ps.setString(2,rentalRecord.getEnd_date());
        ps.setInt(3,rentalRecord.getRecord_id());
        int count = ps.executeUpdate();
        //判断结果
        if(count>0){
            return true;
        }
        //关闭资源
        JDBCUtils.release(rentalRecordDAOImplConn,ps,null);
        return false;

    }

    //查询订单返回订单信息
    @Override
    public RentalRecord SearchRental(RentalRecord order) throws Exception {

        //获取连接
        Connection rentalRecordDAOImplConn = JDBCUtils.getMyConnection();
        String sql;
        PreparedStatement ps;
        ResultSet rs;
        //查询SQL
        sql = "select * from rentalrecord where record_id = ?";
        ps = rentalRecordDAOImplConn.prepareStatement(sql);
        //占位符的赋值
        ps.setInt(1,order.getRecord_id());
        rs = ps.executeQuery();
        RentalRecord rentalRecord = new RentalRecord();
        //判断结果
        if (rs.next()){
            rentalRecord.setRecord_id(rs.getInt(1));
            rentalRecord.setCustomer_id(rs.getInt(2));
            rentalRecord.setCar_id(rs.getInt(3));
            rentalRecord.setStart_date(rs.getString(4));
            rentalRecord.setEnd_date(rs.getString(5));
            rentalRecord.setRental_fee(rs.getInt(6));
            rentalRecord.setRental_status(rs.getString(7));
            rentalRecord.setRental_days(rs.getInt(8));
        }
        //关闭资源
        JDBCUtils.release(rentalRecordDAOImplConn,ps,rs);
        return rentalRecord;
    }

    //查询所有的租车信息
    @Override
    public List<RentalRecord> getAllRental() throws Exception {
        //获取连接
        Connection rentalRecordDAOImplConn = JDBCUtils.getMyConnection();
        String sql;
        PreparedStatement ps;
        ResultSet rs;

        List<RentalRecord> rentalList = new ArrayList<>();

        sql = "select * from rentalrecord";
        ps = rentalRecordDAOImplConn.prepareStatement(sql);
        rs = ps.executeQuery();
        while (rs.next()){
            RentalRecord rentalRecord = new RentalRecord();
            rentalRecord.setRecord_id(rs.getInt("record_id"));
            rentalRecord.setCustomer_id(rs.getInt("customer_id"));
            rentalRecord.setCar_id(rs.getInt("car_id"));
            rentalRecord.setStart_date(rs.getString("start_date"));
            rentalRecord.setEnd_date(rs.getString("end_date"));  // 数据库中存储的是日期类型
            rentalRecord.setRental_fee(rs.getInt("rental_fee"));
            rentalRecord.setRental_status(rs.getString("rental_status"));
            rentalRecord.setRental_days(rs.getInt("rental_days"));
            rentalList.add(rentalRecord);
        }

        return rentalList;
    }

    //根据id修改订单状态，管理员界面
    @Override
    public void updatestatus(int quit_rentalid) throws Exception {
        //获取连接
        Connection rentalRecordDAOImplConn = JDBCUtils.getMyConnection();
        String sql;
        PreparedStatement ps;
        ResultSet rs;
        sql = "UPDATE rentalrecord SET rental_status = '已取消' WHERE record_id = ?";
        ps = rentalRecordDAOImplConn.prepareStatement(sql);
        ps.setInt(1, quit_rentalid); // 设置第一个问号
        ps.executeUpdate(); // 执行更新操作
    }

    //根据车辆id查询数据，管理员界面
    @Override
    public List<RentalRecord> getRental_ByCarid(int searchText) throws Exception {

        //获取连接
        Connection rentalRecordDAOImplConn = JDBCUtils.getMyConnection();
        String sql;
        PreparedStatement ps;
        ResultSet rs;

        List<RentalRecord> rentalList = new ArrayList<>();
        sql = "select * from rentalrecord where car_id = ?";
        ps = rentalRecordDAOImplConn.prepareStatement(sql);
        ps.setInt(1, searchText); // 设置第一个问号

        rs = ps.executeQuery();
        while (rs.next()){
            RentalRecord rentalRecord = new RentalRecord();
            rentalRecord.setRecord_id(rs.getInt("record_id"));
            rentalRecord.setCustomer_id(rs.getInt("customer_id"));
            rentalRecord.setCar_id(rs.getInt("car_id"));
            rentalRecord.setStart_date(rs.getString("start_date"));
            rentalRecord.setEnd_date(rs.getString("end_date"));  // 数据库中存储的是日期类型
            rentalRecord.setRental_fee(rs.getInt("rental_fee"));
            rentalRecord.setRental_status(rs.getString("rental_status"));
            rentalRecord.setRental_days(rs.getInt("rental_days"));
            rentalList.add(rentalRecord);
        }
        return rentalList;
    }

}
